import { request } from './index'
import type { 
  ApiResponse,
  TextContentModel,
  GeneratedImageModel,
  TextGenerationRequest,
  ImageGenerationRequest,
  BatchImageGenerationRequest
} from '@/types'

// AI识别相关API
export const aiApi = {
  // 触发AI图片识别
  triggerRecognition(imageId: number): Promise<ApiResponse> {
    return request.post(`/ai/recognition/trigger/${imageId}`)
  },

  // 获取AI识别结果
  getRecognitionResult(imageId: number): Promise<ApiResponse> {
    return request.get(`/ai/recognition/result/${imageId}`)
  },

  // 批量触发AI识别
  triggerBatchRecognition(imageIds: number[]): Promise<ApiResponse> {
    return request.post('/ai/recognition/batch-trigger', { imageIds })
  },

  // 重新触发AI识别
  retryRecognition(imageId: number): Promise<ApiResponse> {
    return request.post(`/ai/recognition/retry/${imageId}`)
  }
}

// 文案生成相关API
export const textGenerationApi = {
  // 生成文案
  generateText(requestData: TextGenerationRequest): Promise<ApiResponse<TextContentModel>> {
    return request.post('/ai/text/generate', requestData)
  },

  // 获取文案生成历史
  getTextHistory(imageId: number): Promise<ApiResponse<TextContentModel[]>> {
    return request.get(`/ai/text/history/${imageId}`)
  },

  // 删除生成的文案
  deleteText(textId: number): Promise<ApiResponse> {
    return request.delete(`/ai/text/${textId}`)
  },

  // 更新文案内容
  updateText(textId: number, content: string): Promise<ApiResponse<TextContentModel>> {
    return request.put(`/ai/text/${textId}`, { content })
  },

  // 获取文案生成统计
  getTextStatistics(): Promise<ApiResponse> {
    return request.get('/ai/text/statistics')
  }
}

// 图片生成相关API - 直接调用阿里云API，不插入数据库记录
export const imageGenerationApi = {
  // 生成相似图片 - 直接返回生成结果
  generateSimilarImages(requestData: ImageGenerationRequest): Promise<ApiResponse<{
    success: boolean,
    generatedImageUrls: string[],
    baseImageUrl: string,
    requestedCount: number,
    actualCount: number,
    style: string
  }>> {
    return request.post('/api/image-generation/generate', requestData, {
      headers: {
        'Content-Type': 'application/json'
      }
    })
  },

  // 批量生成相似图片 - 直接返回所有生成结果
  batchGenerateSimilarImages(requestData: BatchImageGenerationRequest): Promise<ApiResponse<{
    data: Array<{
      success: boolean,
      generatedImageUrls: string[],
      baseImageUrl: string,
      requestedCount: number,
      actualCount: number,
      style: string,
      errorMessage: string
    }>,
    summary: {
      totalRequested: number,
      totalProcessed: number,
      successCount: number,
      failedCount: number,
      successRate: number
    }
  }>> {
    return request.post('/api/image-generation/generate/batch', requestData, {
      headers: {
        'Content-Type': 'application/json'
      }
    })
  },



  // 测试阿里云连接
  testAlibabaConnection(): Promise<ApiResponse> {
    return request.get('/api/image-generation/test-connection')
  }
}

// 便捷导出函数
export const triggerAiRecognition = (imageId: number): Promise<ApiResponse> => {
  return aiApi.triggerRecognition(imageId)
}

export const generateTextContent = (requestData: TextGenerationRequest): Promise<ApiResponse<TextContentModel>> => {
  return textGenerationApi.generateText(requestData)
}

export const generateSimilarImages = (requestData: ImageGenerationRequest) => {
  return imageGenerationApi.generateSimilarImages(requestData)
}